package com.cn.kehong.service;

import com.cn.kehong.api.ILaborAttendRecordService;
import com.cn.kehong.dao.LaborAttendDetailMapper;
import com.cn.kehong.dao.LaborAttendRecordMapper;
import com.cn.kehong.domain.LaborAttendDetail;
import com.cn.kehong.domain.LaborAttendDetailExample;
import com.cn.kehong.domain.LaborAttendRecord;
import com.cn.kehong.domain.LaborAttendRecordExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LaborAttendRecordServiceImpl implements ILaborAttendRecordService {

    @Autowired
    private LaborAttendRecordMapper laborAttendRecordMapper;

    @Autowired
    private LaborAttendDetailMapper laborAttendDetailMapper;

    @Override
    public LaborAttendRecord getLaborAttendRecord(String laborId) throws Exception {
        if(StringUtils.isEmpty(laborId)){
            return new LaborAttendRecord();
        }
        List<LaborAttendRecord> laborAttendRecords = laborAttendRecordMapper.selectLaborAttendRecords(laborId);
        if(CollectionUtils.isEmpty(laborAttendRecords)||laborAttendRecords.size() > 1){
            throw new Exception("未查询到记录");
        }
        LaborAttendRecord record = laborAttendRecords.get(0);
        return record;
    }

    @Override
    public String addLaborAttendRecord(LaborAttendRecord laborAttendRecord) throws Exception {
        if(StringUtils.isEmpty(laborAttendRecord.getLaborId())){
            return "参数错误！";
        }
        String attendId = CommonUtil.generateRandomNum("attend-");
        laborAttendRecord.setId(attendId);
        List<LaborAttendDetail> laborAttendDetailList = laborAttendRecord.getLaborAttendDetails();
        if(CollectionUtils.isEmpty(laborAttendDetailList)){
            return "数组长度为空";
        }
        for(LaborAttendDetail detail:laborAttendDetailList){
            detail.setAttendId(attendId);
        }
        int count = laborAttendRecordMapper.insertSelective(laborAttendRecord);
        if(count <1){
            return "插入数据库异常！";
        }
        count = laborAttendDetailMapper.batchInsertAttendDetail(laborAttendDetailList);
        if(count < 1){
            throw new Exception("插入数据库异常");
        }
        return "";
    }

    @Override
    public String updateLaborAttendRecord(LaborAttendRecord laborAttendRecord) throws Exception {
        String id = laborAttendRecord.getId();
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        List<LaborAttendDetail> laborAttendDetailList = laborAttendRecord.getLaborAttendDetails();
        if(CollectionUtils.isEmpty(laborAttendDetailList)){
            return "数组长度为空";
        }
        LaborAttendDetailExample example = new LaborAttendDetailExample();
        example.createCriteria().andAttendIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);
        int count = laborAttendDetailMapper.deleteByExample(example);
        if(count < 0){
            return "清理数据库异常！";
        }
        LaborAttendRecordExample example1 = new LaborAttendRecordExample();
        example1.createCriteria().andIdEqualTo(id);
        count = laborAttendRecordMapper.updateByExampleSelective(laborAttendRecord,example1);
        if(count <1){
            throw new Exception("更新数据异常1");
        }
        count = laborAttendDetailMapper.batchInsertAttendDetail(laborAttendDetailList);
        if(count < 1){
            throw new Exception("插入数据库异常");
        }
        return "";
    }
}
